import redis
from django.conf import settings

class RedisClient:
    """Redis连接单例类，避免重复创建连接"""
    _instance = None

    def __new__(cls):
        if not cls._instance:
            # 从Django配置读取Redis参数
            redis_config = settings.REDIS_CONFIG
            cls.pool = redis.ConnectionPool(
                host=redis_config["host"],
                port=redis_config["port"],
                db=redis_config["db"],
                # password=redis_config["password"],
                # max_connections=redis_config["max_connections"]
            )
            cls._instance = super().__new__(cls)
            # 创建Redis客户端实例（基于连接池）
            cls._instance.client = redis.Redis(connection_pool=cls.pool)
        return cls._instance

    def sunion(self, *args, **kwargs):
        """封装 sunion 方法，调用底层 Redis 客户端实例"""
        return self.client.sunion(*args, **kwargs)  # 修正：调用client的sunion

# 全局Redis客户端
redis_client = RedisClient().client